<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiKecheng extends ApiCommon{

	public function formatproduct($product){
        $product['price_origin'] = $product['price'];
		if(!$this->member) return $product;
		if($product['lvprice']==1){
            $lvprice_data = json_decode($product['lvprice_data'],true);
            $product['price'] = $lvprice_data[$this->member['levelid']]["money_price"];
		}else{
			$memberlevel = Db::name('member_level')->where('id',$this->member['levelid'])->find();
			if(!$memberlevel) return $product;
			if($memberlevel['kecheng_discount'] > 0 && $memberlevel['kecheng_discount'] < 10){
				$product['market_price'] = $product['price'];
				$product['price'] = $product['price'] * $memberlevel['kecheng_discount'] * 0.1;
			}
		}
		return $product;
	}
	public function formatprolist($datalist){
		if($this->member){
            //$kechengset = Db::name('kecheng_sysset')->where('aid',aid)->find();
			$memberlevel = Db::name('member_level')->where('id',$this->member['levelid'])->find();
			foreach($datalist as $k=>$v){
				if($v['lvprice']==1){
					 $lvprice_data = json_decode($v['lvprice_data'],true);
					 $datalist[$k]['price'] = $lvprice_data[$this->member['levelid']]["money_price"];
				}else{
					if($memberlevel['kecheng_discount'] > 0 && $memberlevel['kecheng_discount'] < 10){
						$datalist[$k]['market_price'] = $v['price'];
						$datalist[$k]['price'] = $v['price'] * $memberlevel['kecheng_discount'] * 0.1;
					}
				}
//                if($kechengset['show_buyed_kecheng_price'] == 0){
//                    $datalist[$k]['price'] = '已购买';
//                }
			}
		}
		return $datalist;
	}

	public function getlist(){
		$where = [];
		$where[] = ['aid','=',aid];
		//$where[] = ['status','=',1];
		$where[] = Db::raw("`status`=1");
		
		$bid = input('param.bid') ? input('param.bid') : 0;
		if(input('param.bid')){
			$where[] = ['bid','=',input('param.bid/d')];
		}elseif(false){}else{
			$where[] = ['bid','=',0];
		}
		if(input('param.field') && input('param.order')){
			$order = input('param.field').' '.input('param.order').',sort,id desc';
		}else{
			$order = 'sort desc,id desc';
		}
		//分类 
		if(input('param.cid')){
			$cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
			//子分类
			$clist = Db::name('kecheng_category')->where('aid',aid)->where('pid',$cid)->column('id');
			if($clist){
				$clist2 = Db::name('kecheng_category')->where('aid',aid)->where('pid','in',$clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid]);
				if($cCate){
					$whereCid = [];
					foreach($cCate as $k => $c2){
						$whereCid[] = "find_in_set({$c2},cid) or find_in_set({$c2},pcid)";
					}
                    $where[] = Db::raw(implode(' or ',$whereCid));
				}
			} else {
                $where[] = Db::raw("find_in_set(".$cid.",cid) or find_in_set(".$cid.",pcid)");
            }
		}
		if(input('param.keyword')){
			$where[] = ['name','like','%'.input('param.keyword').'%'];
		}
		$pernum = 10;
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$datalist = Db::name('kecheng_list')->field("id,pic,name,price,join_num,kctype,lvprice,lvprice_data")->where($where)->page($pagenum,$pernum)->order($order)->select()->toArray();
		foreach($datalist as &$d){
			//查看共有几节课
			$d['count'] = 0+Db::name('kecheng_chapter')->where(['aid'=>aid,'kcid'=>$d['id']])->count();
		}
		if(!$datalist) $datalist = [];
		$datalist = $this->formatprolist($datalist);
		return $this->json(['status'=>1,'data'=>$datalist]);
	}
    public function getmululist(){
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = Db::raw("`status`=1");
		$order = 'sort desc,id asc';
		if(input('param.keyword')){
			$where[] = ['name','like','%'.input('param.keyword').'%'];
		}
		$where[] = ['kcid','=',input('param.id')];
		$pernum = 10;
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$datalist = Db::name('kecheng_chapter')->field("id,pic,name,kctype,ismianfei,kcid,video_duration,video_url")->where($where)->page($pagenum,$pernum)->order($order)->select()->toArray();
		if(!$datalist) $datalist = [];
		//$datalist = $this->formatprolist($datalist);
		//查看是否已经学完
		foreach($datalist as $k=>$d){
			 $study = Db::name('kecheng_studylog')->field('id,status,currentTime,jindu')->where('aid',aid)->where('mid',mid)->where('mlid',$d['id'])->find();
			if($d['video_duration']>0){
				$datalist[$k]['duration'] = $this->changeTimeType($d['video_duration']);
			}
			$datalist[$k]['currentTime'] = $study['currentTime'];
			$datalist[$k]['jindu'] = $study['jindu'];
			if($study['status']==1)	$datalist[$k]['jindu'] = '100';

			$datalist[$k]['key'] = $k;
		}
		return $this->json(['status'=>1,'data'=>$datalist]);
	}
	public function list(){
		$bid = input('param.bid') ? input('param.bid') : 0;
		//分类
		if(input('param.cid')){
			$cid = input('param.cid/d');
			//查询分类
			$cids = [$cid];
			//查询子分类
			$childs = Db::name('kecheng_category')->where('aid',aid)->where('pid',$cid)->where('status',1)->column('id');
			if($childs){
				$cids = array_merge($cids,$childs);
			}
			$clist = Db::name('kecheng_category')->where('id','in',$cids)->where('aid',aid)->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$clist) $clist = [];
		}else{
			$clist = Db::name('kecheng_category')->where('aid',aid)->where('bid',$bid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$clist) $clist = [];
		}
        $kechengset = Db::name('kecheng_sysset')->field('show_join_num')->where('aid',aid)->find();
		return $this->json(['clist'=>$clist, 'sysset'=>$kechengset]);
	}
	//三级分类 平台的为一二级 商家的为三级
	public function category3(){
		$list = Db::name('kecheng_category')->where('aid',aid)->where('bid',0)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
		$rdata = [];
		$rdata['data'] = $list;
		return $this->json($rdata);
	}
	//获取二三分类
	public function getdownclist3(){
		$pid = input('param.id/d');
		$clist = Db::name('kecheng_category')->where('aid',aid)->where('pid',$pid)->where('status',1)->order('sort desc,id')->select()->toArray();
		if(!$clist) $clist = [];
		foreach($clist as $k=>$v){
			$rs = Db::name('kecheng_category')->where('aid',aid)->where('bid','>',0)->where('pid',0)->where('pcid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$rs) $rs = array();
			$clist[$k]['child'] = $rs;
		}
		return $this->json(['status'=>1,'data'=>$clist]);
	}
	
	//课程详情
	public function detail(){
		$proid = input('param.id/d');
		$product = Db::name('kecheng_list')->where('id',$proid)->where('aid',aid)->find();
		if(!$product) return $this->json(['status'=>0,'msg'=>'课程不存在']);
		if($product['status']==0) return $this->json(['status'=>0,'msg'=>'课程未上架']);

		if(!$product['pics']) $product['pics'] = $product['pic'];
		$product['pics'] = explode(',',$product['pics']);
		$product = $this->formatproduct($product);

		//是否收藏
		$rs = Db::name('member_favorite')->where('aid',aid)->where('mid',mid)->where('proid',$proid)->where('type','kecheng')->find();
		if($rs){
			$isfavorite = true;
		}else{
			$isfavorite = false;
		}
		//查看共有几节课
		$count = 0 + Db::name('kecheng_chapter')->where(['aid'=>aid,'kcid'=>$proid])->count();

		//添加浏览历史
		$sysset = Db::name('admin_set')->where('aid',aid)->field('name,logo,desc,fxjiesuantype,tel,kfurl,gzts,ddbb')->find();

		if($product['bid']!=0){
			$business = Db::name('business')->where('aid',aid)->where('id',$product['bid'])->field('id,name,logo,desc,tel,address,sales,kfurl')->find();
		}else{
			$business = $sysset;
		}
		$product['detail'] = \app\common\System::initpagecontent($product['detail'],aid,mid,platform);

         //判断用户级别
        //获取当前用户的信息
        $member =  Db::name('member')->where('aid',aid)->where('id',mid)->find();
        $kechengset = Db::name('kecheng_sysset')->where('aid',aid)->find();
		if(!$kechengset) $kechengset = [];

		if($kechengset['details_rec']){
			$tjwhere = [];
			$tjwhere[] = ['aid','=',aid];
			$tjwhere[] = ['status','=',1];

			if($product['bid']){
				$tjwhere[] = ['bid','=',$product['bid']];
			}else{
				$business_sysset = Db::name('business_sysset')->where('aid',aid)->find();
				if(!$business_sysset || $business_sysset['status']==0 || $business_sysset['product_isshow']==0){
					$tjwhere[] = ['bid','=',0];
				}
			}
			
			$tjdatalist = Db::name('kecheng_list')->where($tjwhere)->limit(6)->order(Db::raw('rand()'))->select()->toArray();
			if(!$tjdatalist) $tjdatalist = array();
			$tjdatalist = $this->formatprolist($tjdatalist);
			foreach($tjdatalist as &$t){
				$t['count'] = 0 + Db::name('kecheng_chapter')->where(['aid'=>aid,'kcid'=>$t['id']])->count();
			}
		}else{
			$tjdatalist = [];
		}
       
     
        if($this->member){
			$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
		}
        //升级到下一等级预计节省多少钱
		if($kechengset['show_lvupsavemoney'] == 1 && $this->member){
			$upsavemoney = 0;
			$nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$userlevel['sort'])->order('sort,id')->find();
			if($nextlevel){
				$sell_price = $product['price'];
				if($product['lvprice']==0 && $product['no_discount'] == 0 && $userlevel['discount'] > 0 && $userlevel['discount'] < 10){
					$this_sell_price = $sell_price * $userlevel['discount'] * 0.1;
				}else{
					if($product['lvprice']==1){
						$lvprice_data = json_decode($product['lvprice_data'],true);
						if($lvprice_data && isset($lvprice_data[$this->member['levelid']])){
							$this_sell_price = $lvprice_data[$this->member['levelid']];
						}
					}else{
						$this_sell_price = $sell_price;
					}
				}
				if($product['lvprice']==0 && $product['no_discount'] == 0 && $nextlevel['discount'] > 0 && $nextlevel['discount'] < 10){
					$next_sell_price = $sell_price * $nextlevel['discount'] * 0.1;
				}else{
					if($product['lvprice']==1){
						$lvprice_data = json_decode($product['lvprice_data'],true);
						if($lvprice_data && isset($lvprice_data[$nextlevel['id']])){
							$next_sell_price = $lvprice_data[$nextlevel['id']];
						}
					}else{
						$next_sell_price = $sell_price;
					}
				}
				if($this_sell_price > $next_sell_price){
					$upsavemoney = round($this_sell_price["money_price"] - $next_sell_price["money_price"],2);
				}
			}
			$product['upsavemoney'] = $upsavemoney;
			$product['nextlevelname'] = $nextlevel ? $nextlevel['name'] : '';
            //替换升级文字
            $textReplaceArr = [
				'[等级名称]'=>$product['nextlevelname'],
				'[优惠金额]'=>$product['upsavemoney'],
			];
            $upgrade_text = $kechengset['upgrade_text'];
            foreach($textReplaceArr as $key=>$v){
               if(strpos($upgrade_text,$key)){
                   $upgrade_text = str_replace($key,$v,$upgrade_text);
               }
		    } 
            
            $product['upgrade_text'] = $upgrade_text;
		}else{
			$product['upsavemoney'] = 0;
		}

        $sysset = Db::name('admin_set')->where('aid',aid)->field('name,logo,desc,fxjiesuantype,tel,kfurl,gzts,ddbb')->find();
        //预计佣金
		$commission = 0;
        $product['commission_desc'] = '元';
		if($this->member && $kechengset['showcommission']==1){
			if($userlevel['can_agent']!=0){
				if($product['commissionset']==1){//按比例
					$commissiondata = json_decode($product['commissiondata1'],true);
					if($commissiondata){
						$commission = $commissiondata[$userlevel['id']]['commission1'] * ($product['price_origin'] - ($sysset['fxjiesuantype']==2 ? 0 : 0)) * 0.01;
					}
				}elseif($product['commissionset']==2){//按固定金额
					$commissiondata = json_decode($product['commissiondata2'],true);
					if($commissiondata){
						$commission = $commissiondata[$userlevel['id']]['commission1'];
					}
                }elseif($product['commissionset']==3) {//提成是积分
                    $commissiondata = json_decode($product['commissiondata3'],true);
                    if($commissiondata){
                        $commission = $commissiondata[$userlevel['id']]['commission1'];
                    }
                    $product['commission_desc'] = t('积分');
               
                }elseif($product['commissionset']==0){//按会员等级
				    //fxjiesuantype 0按商品价格,1按成交价格,2按销售利润
					if($userlevel['commissiontype']==1){ //固定金额按单
						$commission = $userlevel['commission1'];
					}else{
						$commission = $userlevel['commission1'] * ($product['price_origin'] - ($sysset['fxjiesuantype']==2 ? 0 : 0)) * 0.01;
					}
				}
				if($product['commissionset4']==1 && $product['lvprice']==1){ //极差分销
					$lvprice_data = json_decode($product['lvprice_data'],true);
					$commission += array_shift($lvprice_data) - $product['price'];
					if($commission < 0) $commission = 0;
				}
			}
		}
		$product['commission'] = round($commission*100)/100;
		$sysset['showgzts'] = false;
        $sysset['show_join_num'] = $kechengset['show_join_num'];
		//关注提示
		if(platform == 'mp'){
			$sysset['gzts'] = explode(',',$sysset['gzts']);
			if(in_array('2',$sysset['gzts']) && $this->member['subscribe']==0){
				$appinfo = \app\common\System::appinfo(aid,'mp');
				$sysset['qrcode'] = $appinfo['qrcode'];
				$sysset['gzhname'] = $appinfo['nickname'];
				$sysset['showgzts'] = true;
			}
		}
		//查看是否购买
        if(mid)
		    $order =  Db::name('kecheng_order')->where('aid',aid)->where('kcid',$proid)->where('mid',mid)->where('status',1)->order('id','desc')->find();
		$product['ispay'] = 0;
		if($order && $order['status']==1){
            //如果当时会员免费，现在的会员等级收费，依然需要付费
            if($order['totalprice'] == 0 && $product['price'] > 0){
                $product['ispay'] = 0;
            }else{
                $product['ispay'] = 1;
            }
		}
		$product['count'] = $count;
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['title'] = $product['name'];
		$rdata['sysset'] = $sysset;
		$rdata['isfavorite'] = $isfavorite;
		$rdata['product'] = $product;
		$rdata['business'] = $business;
		$rdata['tjdatalist'] = $tjdatalist;
		$rdata['kechengset'] = $kechengset;
		return $this->json($rdata);
	}
	//课程章节详情
	public function mldetail(){
		$this->checklogin();
		$proid = input('param.id/d');
		$kcid = input('param.kcid/d');
		$kc =  Db::name('kecheng_list')->where('id',$kcid)->find();
		$kc = $this->formatproduct($kc);
		//查看我的课程里面有没有
		$order = Db::name('kecheng_order')->where('kcid',$kcid)->where('aid',aid)->where('mid',mid)->where('status',1)->order('id','desc')->find();
		if(!$proid){
			//章节id不存在，看studylog 有没有
			$studylog = Db::name('kecheng_studylog')->where('aid',aid)->where('mid',mid)->where('kcid',$kcid)->where('status',0)->order('id desc')->find();
			if($studylog){
				 $detail = Db::name('kecheng_chapter')->where('id',$studylog['mlid'])->where('aid',aid)->find();
			}else{
				//查看有没有已经学完的
				$studylog = Db::name('kecheng_studylog')->where('aid',aid)->where('mid',mid)->where('kcid',$kcid)->where('status',1)->order('id desc')->find();
				if($studylog){
					$detail = Db::name('kecheng_chapter')->where('kcid',$kcid)->where('aid',aid)->where('id','>',$studylog['mlid'])->order('id asc')->where('status',1)->find();
					if(!$detail) $detail = Db::name('kecheng_chapter')->where('kcid',$kcid)->where('aid',aid)->where('status',1)->order('sort desc,id asc')->find();
				}else{
					$detail = Db::name('kecheng_chapter')->where('kcid',$kcid)->where('aid',aid)->where('status',1)->order('sort desc,id asc')->find();
				}
				$this->addstudys($kcid,$detail);
				//没有订单的课程，学习增加销量
                \app\model\Payorder::addSales(0,'kecheng',aid,$detail['bid'],1);
				$studylog = Db::name('kecheng_studylog')->where('aid',aid)->where('mid',mid)->where('kcid',$kcid)->where('mlid',$detail['id'])->order('id desc')->find();
			}
			if(!$detail) return $this->json(['status'=>-4,'msg'=>'暂无章节']);
			if($kc['price']==0 && !$order ){
				$ordernum = date('ymdHis').rand(100000,999999);
				$data1 = [];
				$data1['aid'] = aid;
				$data1['mid'] = mid;
				$data1['bid'] = $kc['bid'];
				$data1['pic'] = $kc['pic'];
				$data1['createtime'] = time();
				$data1['ordernum'] = $ordernum;
				$data1['platform'] = platform;
				$data1['title'] = $kc['name'];
				$data1['kcid'] = $kcid;
				$data1['totalprice'] = $kc['price'];
				$data1['price'] = $kc['price'];
				$data1['status'] = 1;
				//增加学习记录
				$order = Db::name('kecheng_order')->insertGetId($data1);
				if($order) Db::name('kecheng_list')->where('aid',aid)->where('id',$kcid)->inc('join_num')->update();
			}elseif($kc['price']>0 && !$order){
				 return $this->json(['status'=>-4,'msg'=>'请先购买课程','url'=>'/activity/kecheng/product?id='.$kcid]);
			}elseif ($kc['price']>0 && $order['totalprice'] == 0){
                //如果当时会员免费，现在的会员等级收费，依然需要付费
                return $this->json(['status'=>-4,'msg'=>'请先购买课程','url'=>'/activity/kecheng/product?id='.$kcid]);
            }

		}else{
			$studylog = Db::name('kecheng_studylog')->where('aid',aid)->where('mid',mid)->where('kcid',$kcid)->where('mlid',$proid)->order('id desc')->find();
			$detail = Db::name('kecheng_chapter')->where('id',$proid)->where('aid',aid)->where('status',1)->find();
            if(!$detail) return $this->json(['status'=>-4,'msg'=>'章节不存在']);
            if(!$studylog){
				$this->addstudys($kcid,$detail);
			}
			$studylog = Db::name('kecheng_studylog')->where('aid',aid)->where('mid',mid)->where('kcid',$kcid)->where('mlid',$proid)->order('id desc')->find();
		}
        if(!$detail['ismianfei'] && $kc['price']>0 && !$order) return $this->json(['status'=>-4,'msg'=>'请先购买课程']);
		if($detail['status']==0) return $this->json(['status'=>-4,'msg'=>'章节未上架']);
        //获取上次播放时间
		if($detail['kctype']>1){
			$detail['startTime'] = $studylog['currentTime']?$studylog['currentTime']:0;
		}
		//查看是否购买
		$detail['ispay'] = 0;
		if($order && $order['status']==1){
			$detail['ispay'] = 1;
		}
		//根据课程查询是否已学完
		$detail['count'] =0 + Db::name('kecheng_studylog')->where(['kcid'=>$detail['kcid'],'status'=>1,'mid'=>mid,'aid'=>aid])->group('mlid')->count();
		//查看共有几节课
		$detail['kccount'] = 0+Db::name('kecheng_chapter')->where(['aid'=>aid,'kcid'=>$detail['kcid']])->count();
		//
		if($detail['sort']>0){
			$detail['key'] = db('kecheng_chapter')->where('kcid='.$detail['kcid'].' and status=1 and sort>'.$detail['sort'])->order('sort desc,id desc')->count();
		}else{
			$detail['key'] = db('kecheng_chapter')->where('kcid='.$detail['kcid'].' and status=1 and id<'.$detail['id'])->order('sort desc,id desc')->count();
		}
		$speedlist=[];
		$showspeed=false;
		$detail['isdt'] = $kc['isdt'];
		$detail['price'] = $kc['price'];
		$detail['isstudy'] = $studylog['status'];
		$detail['detail'] = \app\common\System::initpagecontent($detail['detail'],aid,mid,platform);
		Db::name('kecheng_list')->where('id',$detail['kcid'])->inc('readnum',1)->update();
        $detail['is_give_score'] = false;
		$rdata = [];
		//查看是否已经考完试
		$record= Db::name('kecheng_record')->where(['aid'=>aid,'kcid'=>$detail['kcid'],'mid'=>mid,'ishg'=>1])->find();
		$rdata['iskaoshi'] = 0;
		$rdata['isauto'] = false;
		if(getcustom('kecheng_dati')) $rdata['isauto'] = true;
		if($record){
			$rdata['isauto'] = false;
			$rdata['iskaoshi'] = 1;
		}
		$rdata['speedlist'] = $speedlist;
		$rdata['showspeed'] = $showspeed;
		$rdata['status'] = 1;
		$rdata['detail'] = $detail;
		$rdata['studylog'] = $studylog;
		return $this->json($rdata);
	}

	//商品海报
	function getposter(){
		$this->checklogin();
		$post = input('post.');
		$platform = platform;
		$page = '/activity/kecheng/product';
		$scene = 'id_'.$post['proid'].'-pid_'.$this->member['id'];
		//if($platform == 'mp' || $platform == 'h5' || $platform == 'app'){
		//	$page = PRE_URL .'/h5/'.aid.'.html#'. $page;
		//}
		$posterset = Db::name('admin_set_poster')->where('aid',aid)->where('type','kecheng')->where('platform',$platform)->order('id')->find();

//		$posterdata = Db::name('member_poster')->where('aid',aid)->where('mid',mid)->where('scene',$scene)->where('type','kecheng')->where('posterid',$posterset['id'])->find();
        //关闭缓存
        if(true || !$posterdata){
			$product = Db::name('kecheng_list')->where('id',$post['proid'])->find();
//			$product = $this->formatproduct($product);
			$sysset = Db::name('admin_set')->where('aid',aid)->find();
			$textReplaceArr = [
				'[头像]'=>$this->member['headimg'],
				'[昵称]'=>$this->member['nickname'],
				'[姓名]'=>$this->member['realname'],
				'[手机号]'=>$this->member['mobile'],
				'[商城名称]'=>$sysset['name'],
				'[课程名称]'=>$product['name'],
				'[课程销售价]'=>$product['price'],
				'[课程市场价]'=>$product['market_price'],
				'[商品图片]'=>$product['pic'],
			];

			$poster = $this->_getposter(aid,$platform,$posterset['content'],$page,$scene,$textReplaceArr);
			$posterdata = [];
			$posterdata['aid'] = aid;
			$posterdata['mid'] = $this->member['id'];
			$posterdata['scene'] = $scene;
			$posterdata['page'] = $page;
			$posterdata['type'] = 'kecheng';
			$posterdata['poster'] = $poster;
            $posterdata['posterid'] = $posterset['id'];
			$posterdata['createtime'] = time();
//			Db::name('member_poster')->insert($posterdata);
		}
		return $this->json(['status'=>1,'poster'=>$posterdata['poster']]);
	}

	public function createOrder(){
		$this->checklogin();
		$sysset = Db::name('admin_set')->where('aid',aid)->find();
		$post = input('post.');
		$ordernum = date('ymdHis').rand(100000,999999);
		$kc =  Db::name('kecheng_list')->where('id',$post['kcid'])->find();
		$kc = $this->formatproduct($kc);
		$data = [];
		$data['aid'] = aid;
		$data['mid'] = mid;
		$data['bid'] = $kc['bid'];
		$data['pic'] = $kc['pic'];
		$data['createtime'] = time();
		$data['ordernum'] = $ordernum;
		$data['platform'] = platform;
		$data['title'] = $kc['name'];
		$data['kcid'] = $post['kcid'];
		$data['totalprice'] = $kc['price'];
		$data['price'] = $kc['price'];
		
		//计算佣金的商品金额
		$commission_totalprice = $data['totalprice'];
		$agleveldata = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
		if($agleveldata['can_agent'] > 0 && $agleveldata['commission1own']==1){
			$this->member['pid'] = mid;
		}
		if($kc['commissionset']!=-1){
			if($this->member['pid']){
				$parent1 = Db::name('member')->where('aid',aid)->where('id',$this->member['pid'])->find();
				if($parent1){
					$agleveldata1 = Db::name('member_level')->where('aid',aid)->where('id',$parent1['levelid'])->find();
					if($agleveldata1['can_agent']!=0){
						$data['parent1'] = $parent1['id'];
					}
				}
			}
			if($parent1['pid']){
				$parent2 = Db::name('member')->where('aid',aid)->where('id',$parent1['pid'])->find();
				if($parent2){
					$agleveldata2 = Db::name('member_level')->where('aid',aid)->where('id',$parent2['levelid'])->find();
					if($agleveldata2['can_agent']>1){
						$data['parent2'] = $parent2['id'];
					}
				}
			}
			if($parent2['pid']){
				$parent3 = Db::name('member')->where('aid',aid)->where('id',$parent2['pid'])->find();
				if($parent3){
					$agleveldata3 = Db::name('member_level')->where('aid',aid)->where('id',$parent3['levelid'])->find();
					if($agleveldata3['can_agent']>2){
						$data['parent3'] = $parent3['id'];
					}
				}
			}
			if($kc['commissionset']==1){//按商品设置的分销比例
				$commissiondata = json_decode($kc['commissiondata1'],true);
				if($commissiondata){
					if($agleveldata1) $data['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $commission_totalprice * 0.01;
					if($agleveldata2) $data['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $commission_totalprice * 0.01;
					if($agleveldata3) $data['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $commission_totalprice * 0.01;
				}
			}elseif($kc['commissionset']==2){//按固定金额
				$commissiondata = json_decode($kc['commissiondata2'],true);
				if($commissiondata){
					if($agleveldata1) $data['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'];
					if($agleveldata2) $data['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'];
					if($agleveldata3) $data['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'];
				}
			}elseif($kc['commissionset']==3){//提成是积分
				$commissiondata = json_decode($kc['commissiondata3'],true);
				if($commissiondata){
					if($agleveldata1) $data['parent1score'] = $commissiondata[$agleveldata1['id']]['commission1'];
					if($agleveldata2) $data['parent2score'] = $commissiondata[$agleveldata2['id']]['commission2'];
					if($agleveldata3) $data['parent3score'] = $commissiondata[$agleveldata3['id']]['commission3'];
				}
			}else{ //按会员等级设置的分销比例
				if($agleveldata1){
					if($agleveldata1['commissiontype']==1){ //固定金额按单
						$data['parent1commission'] = $agleveldata1['commission1'];	
					}else{
						$data['parent1commission'] = $agleveldata1['commission1'] * $commission_totalprice * 0.01;
					}
				}
				if($agleveldata2){
					if($agleveldata2['commissiontype']==1){
						$data['parent2commission'] = $agleveldata2['commission2'];				
					}else{
						$data['parent2commission'] = $agleveldata2['commission2'] * $commission_totalprice * 0.01;
					}
				}
				if($agleveldata3){
					if($agleveldata3['commissiontype']==1){
						$data['parent3commission'] = $agleveldata3['commission3'];
					}else{
						$data['parent3commission'] = $agleveldata3['commission3'] * $commission_totalprice * 0.01;
					}
				}
			}
		}	
		$orderid = Db::name('kecheng_order')->insertGetId($data);
		
		if($data['parent1'] && ($data['parent1commission'] || $data['parent1score'])){
			Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$data['parent1'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$kc['id'],'type'=>'kecheng','commission'=>$data['parent1commission'],'score'=>$data['parent1score'],'remark'=>'下级购买课程奖励','createtime'=>time()]);
		}
		if($data['parent2'] && ($data['parent2commission'] || $data['parent2score'])){
			Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$data['parent2'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$kc['id'],'type'=>'kecheng','commission'=>$data['parent2commission'],'score'=>$data['parent2score'],'remark'=>'下二级购买课程奖励','createtime'=>time()]);
		}
		if($data['parent3'] && ($data['parent3commission'] || $data['parent3score'])){
			Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$data['parent3'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$kc['id'],'type'=>'kecheng','commission'=>$data['parent3commission'],'score'=>$data['parent3score'],'remark'=>'下三级购买课程奖励','createtime'=>time()]);
		}

		$payorderid = \app\model\Payorder::createorder(aid,$data['bid'],$data['mid'],'kecheng',$orderid,$data['ordernum'],$data['title'],$data['totalprice']);

		return $this->json(['status'=>1,'payorderid'=>$payorderid,'msg'=>'提交成功']);

	}

	//订单列表
	function orderlist(){
		$this->checklogin();
		$st = input('param.st');
		$pagenum = input('param.pagenum') ? input('param.pagenum') : 1;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mid','=',mid];
		$where[] = ['status','=',1];
		if(input('param.keyword')){
			$where[] = ['title','like','%'.input('param.keyword').'%'];
		}
		$datalist = Db::name('kecheng_order')->where($where)->order('id desc')->page($pagenum,10)->select()->toArray();
		if(!$datalist) $datalist = [];
		foreach($datalist as $key=>$v){
			if($v['bid']!=0){
				$datalist[$key]['binfo'] = Db::name('business')->where('aid',aid)->where('id',$v['bid'])->field('id,name,logo')->find();
			}
			//根据课程查询是否已学完
			$datalist[$key]['count'] =0 + Db::name('kecheng_studylog')->where(['kcid'=>$v['kcid'],'status'=>1,'mid'=>mid,'aid'=>aid])->group('mlid')->count();
			//查看共有几节课
			$datalist[$key]['kccount'] = 0+Db::name('kecheng_chapter')->where(['aid'=>aid,'kcid'=>$v['kcid']])->count();
			//echo $count;
			//查看是否已经考完试
			$record= Db::name('kecheng_record')->where(['aid'=>aid,'kcid'=>$v['kcid'],'mid'=>mid,'ishg'=>1])->find();
			if($record){
				$datalist[$key]['iskaoshi'] = 1;
			}
			$datalist[$key]['isdt'] = Db::name('kecheng_list')->where(['aid'=>aid,'id'=>$v['kcid']])->value('isdt');
		}
		$rdata = [];
		$rdata['st'] = $st;
		$rdata['datalist'] = $datalist;
		return $this->json($rdata);
	}
	//添加学习记录
	public function addstudy(){
		$this->checklogin();
		$post = input('post.');
		$data = [];
		$logid = $post['logid'];
		$study = Db::name('kecheng_studylog')->where('aid',aid)->where('mid',mid)->where('id',$logid)->find();
		$ml = Db::name('kecheng_chapter')->where('id',$study['mlid'])->find();
	
		$data['createtime'] = time();
		if($post['currentTime']){
			$data['currentTime'] = $post['currentTime'];
		}
		if($ml['kctype']==3 || $ml['kctype']==2){
			
			if($post['playJd'] && $study['status']!=1){
				$data['jindu'] = $post['playJd'];
			}elseif(!$post['playJd'] && $ml['video_duration']>0){
				$data['jindu'] = intval($data['currentTime']/$ml['video_duration']*100);
			}
			if($post['currentTime']>=$ml['video_duration'] || $study['status']==1){
				$data['status'] = 1;
				$data['jindu'] = '100';
			}
		}else{
			$data['status'] = 1;
			$data['jindu'] = '已学完';
		}
		//var_dump($data);
		 if($study && $study['stutus']==0){
			 $orderid = Db::name('kecheng_studylog')->where('id',$study['id'])->update($data);
		 }

		 return $this->json(['status'=>1,'jindu'=>$data['jindu']]);
	}
	public function getTiku(){
		$this->checklogin();
		$post = input('post.');
		//取出课程答题设置
		$set =  Db::name('kecheng_list')->field('id,aid,bid,sxdate,isdt,dtnum')->where('id',$post['kcid'])->find();
		if(!$set) return $this->json(['status'=>-4,'msg'=>'该课程不存在']);
		if($set['isdt']==0) return $this->json(['status'=>-4,'msg'=>'该课程未开启答题']);
		//查看是否合格
		$record= Db::name('kecheng_record')->where(['aid'=>aid,'kcid'=>$post['kcid'],'mid'=>mid,'ishg'=>1])->find();
		if($record) return $this->json(['status'=>-4,'msg'=>'已考试合格，无需再次考试','url'=>'/activity/kecheng/product?id='.$post['kcid']]);	
		//查看是否已经存入
		$record  = Db::name('kecheng_record')->where('aid',aid)->where('kcid',$post['kcid'])->where('mid',mid)->where('status','0')->find();
		//如果存在直接取出题目 否则新增进去
		$dtnum = $set['dtnum'];
		if(!$record){
			//随机取出题目
			$tklist = Db::name('kecheng_tiku')->where('aid',aid)->where('kcid',$post['kcid'])->orderRaw('rand()')->where('status',1)->limit($dtnum)->select()->toArray();
			if(!$tklist) return $this->json(['status'=>-4,'msg'=>'暂没有考试题目']);	
			//将题库id 存入record
			$ids = json_encode(array_column($tklist, 'id'));
			$arr = [];
			$arr['aid'] = aid;
			$arr['mid'] = mid;
			$arr['kcid'] = $post['kcid'];
			$arr['time'] = time();
			$arr['timu'] = $ids;
			$rid = Db::name('kecheng_record')->insertGetId($arr);
			$record  = Db::name('kecheng_record')->where('aid',aid)->where('id',$rid)->find();
		}else{
			$log = Db::name('kecheng_recordlog')->where('aid',aid)->where('mid',mid)->where('recordid',$record['id'])->where('status',0)->order('id desc')->find();
		}
		$count = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$record['id'])->count();
		//取出的题目
		$timu = json_decode($record['timu']);
		$tmid = $timu[$count];
		$nums = count($timu);
		if($nums>$count && !$post['op']){
			if(!$log){
				//将题目存到log表
				$data = [];
				$data['aid'] = aid;
				$data['bid'] = $set['bid'];
				$data['mid'] = mid;
				$data['recordid'] = $record['id'];
				$data['time'] = time();
				$data['kcid'] = $post['kcid'];
				$data['tmid'] = $tmid;
				$data['sort'] = $count+1;
				Db::name('kecheng_recordlog')->insertGetId($data);
				$log = Db::name('kecheng_recordlog')->where('aid',aid)->where('mid',mid)->where('recordid',$record['id'])->where('status',0)->order('id desc')->find();
			}
		}
		$tkdata = Db::name('kecheng_tiku')->field('id,title,type,option_group,right_option')->where('aid',aid)->where('id',$log['tmid'])->where('status',1)->find();
		if($tkdata['type']==1){
			$tkdata['sorts'] = ['A','B','C','D','E','F','G'];
			$option = json_decode($tkdata['option_group'],true);
			//var_dump($option);
			$tkdata['option'] = $option;
			$right_option = explode(',',$tkdata['right_option']);
			$tkdata['right_option'] = $right_option;
			$tkdata['rightcount'] = count($right_option);
		}

		//查看剩余时间
		$endtime = $record['time']+$set['sxdate']*60;
		$data['lefttime'] = $endtime-time();
		if($data['lefttime'] <0){
			$data2 = [];
			$score = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$record['recordid'])->where('status',1)->sum('score');
			$data2['status'] = 1;
			$data2['endtime'] = time();
			$data2['score'] = $score;
			$set =  Db::name('kecheng_list')->field('id,aid,sxdate,isdt,dtnum,hgscore')->where('id',$record['kcid'])->find();
			//查看是否合格
			if($score>=$set['hgscore']){
				$data2['ishg'] = 1;
			}
			$res = Db::name('kecheng_record')->where('aid',aid)->where('id',$record['id'])->update($data2);
			return $this->json(['status'=>2,'rid'=>$record['id']]);
		}
		//答的第几道题目
		$data['tkdata'] = $tkdata;
		$data['rid'] = $record['id'];
		$data['nums'] = $nums;
		$data['set'] = $set;
		$data['hasnum'] = $log['sort'];
		$data['dtid'] = $log['id'];
		return $this->json(['status'=>1,'data'=>$data]);
	}
	//上一题
	public function prevquestion(){
		$this->checklogin();
		$post = input('post.');
		if($post['dtid']){
			$log = Db::name('kecheng_recordlog')->where('aid',aid)->where('id','<',$post['dtid'])->order('id desc')->find();
			 $answer = json_decode($log['answer']);
			if(is_null(json_decode($log['answer']))){
				 $answer = $log['answer'];
			}
			$tkdata = Db::name('kecheng_tiku')->field('id,title,type,option_group,right_option')->where('aid',aid)->where('id',$log['tmid'])->find();
			if($tkdata['type']==1){
				$tkdata['sorts'] = ['A','B','C','D','E','F','G'];
				$option = json_decode($tkdata['option_group'],true);
				//var_dump($option);
				$tkdata['option'] = $option;
				$right_option = explode(',',$tkdata['right_option']);
				$tkdata['right_option'] = $right_option;
				$tkdata['rightcount'] = count($right_option);
			}
		}
		$data = [];
		$data['answer'] = $answer;
		$data['tkdata'] = $tkdata;
		$data['hasnum'] = $log['sort'];
		$data['dtid'] = $log['id'];
		return $this->json(['status'=>1,'data'=>$data]);
	}
	//下一题
	public function nextquestion(){
		$this->checklogin();
		$post = input('post.');
		$dtlog = Db::name('kecheng_recordlog')->where('aid',aid)->where('id',$post['dtid'])->find();
		if(!$dtlog) return $this->json(['status'=>0,'msg'=>'答题记录不存在']);
		$dt_options = $post['right_option'];
		$tiku = Db::name('kecheng_tiku')->where('aid',aid)->where('id',$dtlog['tmid'])->find();
		$data = [];
		$right_option = explode(',',$tiku['right_option']); //后台正确答案
		if($tiku['type']==1 && count($right_option)>1){
			if(empty($dt_options) || count($dt_options) != count($right_option)){
				$is_right=0;
			}else{
				$is_right = 1;
				foreach($dt_options as $d){
					if(!in_array($d,$right_option)){
						$is_right=0;
						break; 
					}
				}
			}
			$data['answer'] = json_encode($dt_options);
		}else{
			if($dt_options == $tiku['right_option']){
				$is_right = 1;
			}
			$data['answer'] = $post['right_option'];
		}
		if($is_right==1){
			$data['status'] = 1;
			$data['score'] = $tiku['score'];
		}else{
			$data['status'] = 2;
			$data['score'] = 0;
		}
		Db::name('kecheng_recordlog')->where('aid',aid)->where('id',$dtlog['id'])->update($data);
		$log = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$dtlog['recordid'])->where('id','>',$post['dtid'])->order('id asc')->find();

		$count = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$dtlog['recordid'])->count();
		//取出的题目
		$record  = Db::name('kecheng_record')->where('aid',aid)->where('id',$dtlog['recordid'])->find();
		$timu = json_decode($record['timu']);
		$tmid = $timu[$count];
		$nums = count($timu);
		if(!$log){
			//将下一题存到log表
			$data = [];
			$data['aid'] = aid;
			$data['bid'] = $tiku['bid'];
			$data['mid'] = mid;
			$data['recordid'] = $dtlog['recordid'];
			$data['time'] = time();
			$data['kcid'] = $dtlog['kcid'];
			$data['tmid'] = $tmid;
			$data['sort'] = $count+1;
			Db::name('kecheng_recordlog')->insertGetId($data);
			$log = Db::name('kecheng_recordlog')->where('aid',aid)->where('mid',mid)->where('recordid',$dtlog['recordid'])->where('status',0)->order('id desc')->find();
		}else{
			$answer = json_decode($log['answer']);
			if(is_null(json_decode($log['answer']))){
				 $answer = $log['answer'];
			}
			//$answer = $log['answer'];
		}
		$tkdata = Db::name('kecheng_tiku')->field('id,title,type,option_group,right_option')->where('aid',aid)->where('id',$log['tmid'])->find();
		if($tkdata['type']==1){
			$tkdata['sorts'] = ['A','B','C','D','E','F','G'];
			$option = json_decode($tkdata['option_group'],true);
			//var_dump($option);
			$tkdata['option'] = $option;
			$right_option = explode(',',$tkdata['right_option']);
			$tkdata['right_option'] = $right_option;
			$tkdata['rightcount'] = count($right_option);
		}
		$data = [];
		$data['tkdata'] = $tkdata;
		$data['hasnum'] = $log['sort'];
		$data['dtid'] = $log['id'];
		$data['hasnum'] = $log['sort'];
		$data['answer'] = $answer;
		return $this->json(['status'=>1,'data'=>$data]);
	}
	public function tofinish(){
		$this->checklogin();
		$post = input('post.');
		$dtlog = Db::name('kecheng_recordlog')->where('aid',aid)->where('id',$post['dtid'])->find();
		$tiku = Db::name('kecheng_tiku')->where('aid',aid)->where('id',$dtlog['tmid'])->find();
		$data = [];
		$right_option = explode(',',$tiku['right_option']); //后台正确答案
		$dt_options = $post['right_option']; //我选的答案
		if($tiku['type']==1 && count($right_option)>1){
			if(empty($dt_options) || count($dt_options) != count($right_option)){
				$is_right=0;
			}else{
				$is_right = 1;
				foreach($dt_options as $d){
					if(!in_array($d,$right_option)){
						$is_right=0;
						break; 
					}
				}
			}
			$data['answer'] = json_encode($dt_options);
		}else{
			if($dt_options == $tiku['right_option']){
				$is_right = 1;
			}
			$data['answer'] = $post['right_option'];
		}
		if($is_right==1){
			$data['status'] = 1;
			$data['score'] = $tiku['score'];
		}else{
			$data['status'] = 2;
			$data['score'] = 0;
		}
		//最后
		Db::name('kecheng_recordlog')->where('aid',aid)->where('id',$dtlog['id'])->update($data);	
		//修改答题状态
		$score = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$dtlog['recordid'])->where('status',1)->sum('score');
		$data2 = [];
		$data2['status'] = 1;
		$data2['endtime'] = time();
		$data2['score'] = $score;
		$set =  Db::name('kecheng_list')->field('id,aid,sxdate,isdt,dtnum,hgscore')->where('id',$dtlog['kcid'])->find();
		//查看是否合格
		if($score>=$set['hgscore']){
			$data2['ishg'] = 1;
		}
		$res = Db::name('kecheng_record')->where('aid',aid)->where('id',$dtlog['recordid'])->update($data2);
		return $this->json(['status'=>1]);
	}

	//答题完成页面
	public function complete(){
		$this->checklogin();
		$post = input('post.');
		$record = Db::name('kecheng_record')->field('id,score,ishg,time,endtime,kcid')->where('aid',aid)->where('id',$post['rid'])->find();
		$time = $record['endtime'] - $record['time'];
		$record['time'] = date('Y-m-d H:i:s',$record['time']);
		$record['endtime'] = date('Y-m-d H:i:s',$record['endtime']);
		//答对题目
		$rightnum = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$record['id'])->where('status',1)->count();
		//答错题目
		$errornum = Db::name('kecheng_recordlog')->where('aid',aid)->where('recordid',$record['id'])->where('status',2)->count();
		$record['rightnum'] = $rightnum;
		$record['errornum'] = $errornum;
		$data = [];
		$record['longtime'] =$this->changeTimeType($time);
		$data['record'] = $record;
		return $this->json(['status'=>1,'data'=>$record]);
	}

	//答题记录
	public function recordlog(){
		$this->checklogin();
		$kcid = input('param.kcid');
		$pagenum = input('param.pagenum') ? input('param.pagenum') : 1;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mid','=',mid];
		$where[] = ['kcid','=',$kcid];
		$where[] = ['status','=',1];
		$datalist = Db::name('kecheng_record')->where($where)->order('id desc')->page($pagenum,10)->select()->toArray();
		//echo db('kecheng_record')->getlastsql();
		if(!$datalist) $datalist = [];
		foreach($datalist as &$d){
			$d['title'] =  Db::name('kecheng_list')->where('id',$d['kcid'])->value('name');
			$d['date'] = date('Y-m-d H:i:s',$d['time']); 
		}
		$rdata = [];
		$rdata['datalist'] = $datalist;
		return $this->json($rdata);
	}
	//错题解析
	public function error(){
		$this->checklogin();
		$rid = input('param.rid');
		$op = input('param.op');
		$logid = input('param.logid');
		$timu = Db::name('kecheng_recordlog')->where(['recordid'=>$rid,'status'=>2])->order('id asc')->find();
		if($op=='down'){
			$timu = Db::name('kecheng_recordlog')->where('id','>',$logid)->where('mid',mid)->where('recordid',$rid)->where('status','2')->find();
		}
		if($op=='up'){
			$timu = Db::name('kecheng_recordlog')->where('id','<',$logid)->where('mid',mid)->where('recordid',$rid)->where('status','2')->order('id desc')->find();
		}
		//var_dump($timu);
		$count = Db::name('kecheng_recordlog')->where(['recordid'=>$rid,'status'=>2])->count();
		$tkdata = Db::name('kecheng_tiku')->where('aid',aid)->where('id',$timu['tmid'])->find();
		$tkdata['nums'] = $count;
		$tkdata['hasnum'] = $count;
		if($tkdata['type']==1){
			$tkdata['sorts'] = ['A','B','C','D','E','F','G'];
			$option = json_decode($tkdata['option_group'],true);
			$tkdata['option'] = $option;
		}
		$tkdata['sort'] = $timu['sort'];
		//判断答案是不是json
		$tkdata['answer'] = $timu['answer'];	
		if(!is_null(json_decode($timu['answer']))){
			$tkdata['answer'] = json_decode($timu['answer'],true);		
			$right_option = explode(',',$tkdata['right_option']);
			$tkdata['rightcount'] = count($right_option);
			$right_options = array();
			foreach($right_option as $d){
				$right_options[] = $tkdata['sorts'][$d];
			}
			$tkdata['right_options'] = implode(',',$right_options);
		}
		$up =  Db::name('kecheng_recordlog')->where('id','<',$timu['id'])->where('mid',mid)->where('recordid',$timu['recordid'])->where('status','2')->order('id desc')->find();
		if($up)  $tkdata['isup'] = 1; 
		$down =  Db::name('kecheng_recordlog')->where('id','>',$timu['id'])->where('mid',mid)->where('recordid',$timu['recordid'])->where('status','2')->order('id asc')->find();
		if($down) $tkdata['isdown'] = 1;
		$tkdata['logid'] = $timu['id'];
		return $this->json(['status'=>1,'data'=>$tkdata]);
	}
		//获取下一节
	public function nextsection(){
		$this->checklogin();
		$id = input('param.id');
		$kcid = input('param.kcid');
		$detail = Db::name('kecheng_chapter')->where('aid',aid)->where('id','>',$id)->where('kcid',$kcid)->order('id asc')->find();
		return $this->json(['status'=>1,'id'=>$detail['id']]);
	}
	public function addstudys($kcid,$detail){
		$data = [];
		$data['aid'] = aid;
		$data['mid'] = mid;
		$data['bid'] = $detail['bid'];
		$data['pic'] = $detail['pic'];
		$data['mlid'] = $detail['id'];
		$data['createtime'] = time();
		$data['platform'] = platform;
		$data['title'] = $detail['name'];
		$data['kcid'] = $kcid;
		if($detail['kctype']==1){
			$data['status'] = 1;
			$data['jindu'] = '已学完';
		}
		$data['currentTime'] = 0;
		Db::name('kecheng_studylog')->insertGetId($data);	

	}

	//获取下一题或者上一题
	public function geterrotiku(){
		$this->checklogin();
		$recordid = input('param.rid');
		$logid = input('param.logid');
		$op = input('param.op');
		if($op=='down'){
			$down =  Db::name('kecheng_recordlog')->where('id','>',$logid)->where('recordid',$recordid)->where('status','2')->find();
			$tkdata = Db::name('kecheng_tiku')->where('aid',aid)->where('id',$down['tmid'])->find();	

		}
	}
	/**
	 * 将秒数转换成时分秒
	 *
	 * @param 秒数 $seconds
	 * @return void
	 */
	function changeTimeType($seconds)
	{
		if ($seconds > 3600) {
			$hours = intval($seconds / 3600);
			$time = $hours . ":" . gmstrftime('%M:%S', $seconds);
		} else {
			$time = gmstrftime('%H:%M:%S', $seconds);
		}
		return $time;
	}
	
	public function givescore(){
	    }
}